// pages/explore/index.js
import http from '../../utils/http.js'
Page({
    data: {
        course:[],
        cityName:'重庆市',
        tabArr:['正念冥想','睡眠助手','舒缓音乐','自然声音'],
        tabIdx:0,
        sId:'', //设置菜单滚动
        cateList:[],
        tabShow:true
    },
    onLoad(options) {
        //分类数据
        getApp().fly.get('classes/ReactCategory').then(res=>{
            console.log(res);
            let {results} = res.data
            let parentArr = results.filter(item=>item.parentId=='0-0')
            parentArr.forEach(item=>{
                let children = results.filter(child=>item.objectId==child.parentId)
                children.unshift({name:'全部',objectId:'all'})
                item.children = children
            })
            this.setData({
                cateList:parentArr
            })
        })
        
        //数据列表        
        getApp().fly.get('classes/ReactCourse').then(res=>{
            this.setData({
                course:res.data.results
            })
        })
    },
    handleDetail(ev){
        console.log('事件对象',ev);
        let {id,item} = ev.currentTarget.dataset
        //打开新页面
        wx.navigateTo({
          url: `/pages/detail/index?id=${id}&num=123`,
        })
        //重定向 【建议少用】
        // wx.redirectTo({
        //   url: '/pages/detail/index',
        // })
        // 重载页面【建议少用】
        // wx.reLaunch({
        //   url: '/pages/detail/index',
        // })
    },
    handleCityList(){
        // this  指向Page对象
        wx.navigateTo({
          url: '/pages/city/index',
          events:{
              changeCity:(name)=>{
                // console.log(name);
                this.setData({
                    cityName:name
                })
              }
          }
        })
    },
    //主菜单
    handleTab(ev){
        let {idx} = ev.currentTarget.dataset
        let sId = `tab-${idx-1}`
        console.log(sId);
        this.setData({
            tabIdx:idx,
            sId,
            tabShow:false
        })
        setTimeout(()=>{
            this.setData({tabShow:true})
        },10)

        //重新发起请求
        let cateLv1 = this.data.cateList[idx].name
        getApp().fly.get('classes/ReactCourse',{
            where:{
                cateLv1
            }
        }).then(res=>{
            this.setData({
                course:res.data.results
            })
        })
    },
    //子菜单
    handleChange(ev){
        console.log(ev);
        let {name} = ev.detail
        let {cateList,tabIdx} = this.data
        let cateLv1 = cateList[tabIdx].name //一级类目
        let where = {cateLv1}
        if(name!='全部'){
            where.cateLv2 = name
        }
        getApp().fly.get('classes/ReactCourse',{
            where
        }).then(res=>{
            this.setData({
                course:res.data.results
            })
        })
    }
})